<?php defined('SYSPATH') or die('No direct script access.');
	
	class Controller_SearchAnalysis extends Controller {
		
		protected $filterModel = NULL;
		
		public function action_index(){
			$this->request->response = View::factory("analysis/advanced-search")->render();
		}
		
		public function action_filterpathologies(){
			$pathologies_double_list = View::factory("analysis/pathology-filter-list");
			$pathologies_double_list->pathologies = Helper_Service::factory("pathology")->getAll();
			$this->request->response = $pathologies_double_list->render();
		}
		
		public function action_search(){
			//armado de los filtros
			
			$this->filterModel = Model::factory("filtersearch");
			
			if (isset($_POST["cmbAODComparative"])) $this->checkAOD();
			if (isset($_POST["cmbAOSComparative"])) $this->checkAOS();
			if (isset($_POST["cmbAGEComparative"])) $this->checkAge();
			if (isset($_POST["cmbDATEComparative"])) $this->checkAnalysisDate();
			if (isset($_POST["cmbIARComparative"])) $this->checkIAR();
			if (isset($_POST["cmbPDComparative"])) $this->checkDiastolicPressure();
			if (isset($_POST["cmbPSComparative"])) $this->checkSystolicPressure();
			if (isset($_POST["pathologies"])) $this->checkPathologies();
			if (isset($_POST["cmbSex"])) $this->checkSex();
			//if (isset($_POST["chkSmoker"]) || isset($_POST["chkSedentary"])) $this->checkHabits();
						
			$query = $this->filterModel->buildQuery();
			Fire::log($query);
			
			$dbquery = DB::query(Database::SELECT, $query)->execute()->as_array();
			$searchResults = View::factory("analysis/search-results");
			$searchResults->dataset = $dbquery;
			$this->request->response = $searchResults->render();
			
		}
		
		
		public function checkAge(){
						
			$filter_elem = Model::factory("filterelement");
			$filter_elem->setGlueOperator(" AND ");
			
			$cmbAge = $_POST["cmbAGEComparative"];
			if ($cmbAge == "more"){ $filter_elem->add("clinical_information.age",">=",$_POST["age_val"]);}
			elseif($cmbAge == "less"){	$filter_elem->add("clinical_information.age","<=",$_POST["age_val"]);}
			else{
				$filter_elem->add("clinical_information.age",">=",$_POST["age_min_val"]);
				$filter_elem->add("clinical_information.age","<=",$_POST["age_max_val"]); 
			}
			
			$this->filterModel->addFilter($filter_elem);
		}
				
		public function checkAnalysisDate(){
						
			$filter_elem = Model::factory("filterelement");
			$filter_elem->setGlueOperator(" AND ");
			
			$cmbDate = $_POST["cmbDATEComparative"];
			if ($cmbDate == "more"){ $filter_elem->add("analysis.analysis_date",">=","'".Helper_Date::dateToMysql($_POST["date_val"])."'");}
			elseif($cmbDate == "less"){	$filter_elem->add("analysis.analysis_date","<=","'".Helper_Date::dateToMysql($_POST["date_val"])."'");}
			else{
				$filter_elem->add("analysis.analysis_date",">=","'".Helper_Date::dateToMysql($_POST["date_min_val"])."'");
				$filter_elem->add("analysis.analysis_date","<=","'".Helper_Date::dateToMysql($_POST["date_max_val"])."'"); 
			}
			
			$this->filterModel->addFilter($filter_elem);
							
		}
		
		public function checkAOD(){
						
			$filter_elem = Model::factory("filterelement");
			$filter_elem->setGlueOperator(" AND ");
			
			$cmbAOD = $_POST["cmbAODComparative"];
			if ($cmbAOD == "more"){ $filter_elem->add("analysis_result.aod_value",">=",$_POST["aod_val"]);}
			elseif($cmbAOD == "less"){	$filter_elem->add("analysis_result.aod_value","<=",$_POST["aod_val"]);}
			else{
				$filter_elem->add("analysis_result.aod_value",">=",$_POST["aod_min_val"]);
				$filter_elem->add("analysis_result.aod_value","<=",$_POST["aod_max_val"]); 
			}
			
			$this->filterModel->addFilter($filter_elem);
		}
		
		public function checkAOS(){
					
			$filter_elem = Model::factory("filterelement");
			$filter_elem->setGlueOperator(" AND ");
			
			$cmbAOS = $_POST["cmbAOSComparative"];
			if ($cmbAOS == "more"){ $filter_elem->add("analysis_result.aos_value",">=",$_POST["aos_val"]);}
			elseif($cmbAOS == "less"){	$filter_elem->add("analysis_result.aos_value","<=",$_POST["aos_val"]);}
			else{
				$filter_elem->add("analysis_result.aos_value",">=",$_POST["aos_min_val"]);
				$filter_elem->add("analysis_result.aos_value","<=",$_POST["aos_max_val"]); 
			}
			
			$this->filterModel->addFilter($filter_elem);
		}
		
		public function checkIAR(){
					
			$filter_elem = Model::factory("filterelement");
			$filter_elem->setGlueOperator("AND");
			
			$cmbIAR = $_POST["cmbIARComparative"];
			if ($cmbIAR == "more"){ $filter_elem->add("analysis_result.iar_value",">=",$_POST["iar_val"]);}
			elseif($cmbIAR == "less"){	$filter_elem->add("analysis_result.iar_value","<=",$_POST["iar_val"]);}
			else{
				$filter_elem->add("analysis_result.iar_value",">=",$_POST["iar_min_val"]);
				$filter_elem->add("analysis_result.iar_value","<=",$_POST["iar_max_val"]); 
			}
			
			$this->filterModel->addFilter($filter_elem);
		}
		
		public function checkSex(){
						
			$filter_elem = Model::factory("filterelement");
			$filter_elem->setGlueOperator(" AND ");
			
			$cmbSex = $_POST["cmbSex"];
			if ($cmbSex == "male"){ $filter_elem->add("patients.sex","=","'M'");}
			else { $filter_elem->add("patients.sex","=","'F'");}
						
			$this->filterModel->addFilter($filter_elem);
		}
		
		public function checkSystolicPressure(){
						
			$filter_elem = Model::factory("filterelement");
			$filter_elem->setGlueOperator(" AND ");
			
			$cmbPS = $_POST["cmbPSComparative"];
			if ($cmbPS == "more"){ $filter_elem->add("clinical_information.systolic_pressure ",">=",$_POST["ps_val"]);}
			elseif($cmbPS == "less"){	$filter_elem->add("clinical_information.systolic_pressure ","<=",$_POST["ps_val"]);}
			else{
				$filter_elem->add("clinical_information.systolic_pressure ",">=",$_POST["ps_min_val"]);
				$filter_elem->add("clinical_information.systolic_pressure ","<=",$_POST["ps_max_val"]); 
			}
			
			$this->filterModel->addFilter($filter_elem);
		}
		
		public function checkDiastolicPressure(){
						
			$filter_elem = Model::factory("filterelement");
			$filter_elem->setGlueOperator(" AND ");
			
			$cmbPD = $_POST["cmbPDComparative"];
			if ($cmbPD == "more"){ $filter_elem->add("clinical_information.diastolic_pressure",">=",$_POST["pd_val"]);}
			elseif($cmbPD == "less"){	$filter_elem->add("clinical_information.diastolic_pressure","<=",$_POST["pd_val"]);}
			else{
				$filter_elem->add("clinical_information.diastolic_pressure",">=",$_POST["pd_min_val"]);
				$filter_elem->add("clinical_information.diastolic_pressure","<=",$_POST["pd_max_val"]); 
			}
			
			$this->filterModel->addFilter($filter_elem);
		}
		
		
		public function checkPathologies(){
						
			$filter_elem = Model::factory("filterelement");
			$filter_elem->setGlueOperator(" OR ");
			
			$pathologies = $_POST["pathologies"];
			if (!$pathologies || !(count($pathologies)>0)){return;}
			for ($i=0; $i < count($pathologies); $i++){	$filter_elem->add("pathology_by_patient.pathology_id","=",$pathologies[$i]);}
					
			$this->filterModel->addFilter($filter_elem);
		}
		
		public function checkHabits(){
						
			$filter_elem = Model::factory("filterelement");
			$filter_elem->setGlueOperator(" AND ");
			
			//$cmbDate = $_POST["cmbDateComparative"];
			//if ($cmbDate == "more"){ $filter_elem->add("analysis.analysis_date",">=",Helper_Date::dateToMysql($_POST["date_val"]));}
			//elseif($cmbDate == "less"){	$filter_elem->add("analysis.analysis_date","<=",Helper_Date::dateToMysql($_POST["date_val"]));}
			//else{
			//	$filter_elem->add("analysis.analysis_date",">=",Helper_Date::dateToMysql($_POST["date_min_val"]));
			//	$filter_elem->add("analysis.analysis_date","<=",Helper_Date::dateToMysql($_POST["date_max_val"])); 
			//}
			
			$this->filterModel->addFilter($filter_elem);
		}
		
		
		
		
		
	}
?>